<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建秒杀场次':'编辑秒杀场次'" :close-on-click-modal="true" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="form" :model="form" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="场次名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="秒杀时间" prop="timeRange">
          <el-date-picker class="w-100"
            v-model="form.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布状态" prop="status">
          <el-select v-model="form.status" placeholder="发布状态">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { seckillSessionLoad, seckillSessionSave } from '@/api/mall/seckill/session'

export default {
  name: 'SeckillSessionDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        name: undefined,
        startTime: undefined,
        endTime: undefined,
        timeRange: [],
        status: '1'
      },
      optionOfStatus: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id
      this.form.timeRange = []
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      seckillSessionLoad({ 'id': id }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        this.form.timeRange.push(this.form.startTime)
        this.form.timeRange.push(this.form.endTime)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['mall:seckill:edit'])
      }).catch(() => {
      })
    },
    getDictData() {
      this.$dictGroup('sys_publish_status').then(response => {
        this.optionOfStatus = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.startTime = this.form.timeRange[0]
          this.form.endTime = this.form.timeRange[1]
          seckillSessionSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.form.resetFields()
      this.visible = false
    }
  }
}
</script>
